package com.hermes.action.login;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.hermes.biz.common.util.login.CorpLoginUtil;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;

public class CorpSinaLoginProcessAction extends BaseAction {

    private static final long    serialVersionUID = 1L;
    private static Logger        logger           = Logger
                                                      .getLogger(CorpSinaLoginProcessAction.class);
    private static CorpLoginUtil clu              = new CorpLoginUtil();
    private UserService          userService;

    @Override
    public String execute() throws IOException {

        //        String codeRes = ParameterParserUtil.getParameter("code");
        //
        //        if (StringUtil.isNotBlank(codeRes)) {
        //            String getATRes = executeSendPost(clu.sinaGetAccessTokenUrl, "&code=" + codeRes);
        //            JSONObject jo = JSONObject.fromObject(getATRes);
        //            String atokenStr = jo.getString("access_token");
        //            String uidStr = jo.getString("uid");
        //            String getUserParamStr = "access_token=" + atokenStr + "&uid=" + uidStr;
        //            String userInfoJsonStr = executeSend(clu.getSinaUserInfoUrl, getUserParamStr);
        //            JSONObject userInfo = JSONObject.fromObject(userInfoJsonStr);
        //            logger.error(userInfoJsonStr);
        //            String userName = userInfo.getString("name");
        //            String userHeadPic = userInfo.getString("profile_image_url");
        //            String userIdStr = userInfo.getString("id");
        //            session.put("userName", userName);
        //
        //            long randomUserId = System.currentTimeMillis();
        //            if (randomUserId <= 0) {
        //                randomUserId = 0 - randomUserId;
        //            }
        //            UserDO userDO = new UserDO();
        //            userDO.setAccessTokenSina(atokenStr);
        //            userDO.setOpenIdSina(userIdStr);
        //            userDO.setUserId(randomUserId);
        //            userDO.setUserName(userName);
        //            userDO.setHeadPic(userHeadPic);
        //            userDO.setUserPassword("888888");
        //
        //            userService.registerNewUserAccount(userDO);
        //
        //            session.put("userId", randomUserId);
        //
        //            HttpServletResponse response = ServletActionContext.getResponse();
        //            response.sendRedirect("/index.htm");
        //        } else {
        //            //杩涜繖閲屾槸鍌婚�
        //            logger.error("nnd");
        //        }
        return null;
    }

    public String executeSend(String url, String param) {
        try {
            String sendURL = url + param;
            HttpClient hc = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(sendURL);
            HttpResponse response = hc.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            logger.error(e);
        }
        return "";

    }

    public String executeSendPost(String url, String param) {
        try {

            String sendURL = url + param;
            HttpClient hc = new DefaultHttpClient();
            HttpPost httpget = new HttpPost(sendURL);
            HttpResponse response = hc.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                return EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            logger.error(e);
        }
        return "";

    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
